package com.iflytek.autofly.setting.biz.networking;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.android.internal.telephony.OperatorInfo;
import com.android.internal.telephony.TelephonyIntents;
import com.android.phone.INetworkQueryService;
import com.android.phone.INetworkQueryServiceCallback;
import com.android.phone.INetworkSelectCallback;
import com.iflytek.autofly.bluetoothservice.util.BluetoothServiceCons;
import com.iflytek.autofly.setting.R;
import com.iflytek.autofly.setting.biz.Notice;
import com.iflytek.autofly.setting.biz.base.BaseFragment;
import com.iflytek.autofly.setting.biz.base.BottomMenu;
import com.iflytek.autofly.setting.biz.base.CommonRadioButtonBean;
import com.iflytek.autofly.setting.biz.base.CommonRadioGroupAdpt;
import com.iflytek.autofly.setting.biz.base.EditActivity;
import com.iflytek.autofly.setting.biz.networking.BluetoothSetting.IBtViewChangedListener;
import com.iflytek.autofly.setting.biz.networking.WlanSetting.IWifiViewChangedListener;
import com.iflytek.autofly.setting.widget.InnerListView;
import com.iflytek.autofly.setting.widget.LoadingAnimView;
import com.iflytek.autofly.utils.FlyLog;
import com.iflytek.sunflower.FlowerCollector;
import com.nineoldandroids.animation.ObjectAnimator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <b>Description: </b>网络设置的fragment<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015年10月14日</td>
 * <td>上午11:34:14</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class NetworkingFragment extends BaseFragment implements
        IWifiViewChangedListener, IBtViewChangedListener {

    protected static final String TAG = "NetworkingFragment";

    private static final String[] CMD = {"bluetooth", "wifi"};
    private static final int REQUEST_BT_NAME = 0x1001;
    private static final int REQUEST_NET_PASSWD = 0x1002;
    private static final int REQUEST_AP_PASSWD = 0x1003;

    private static final String ACTION_CARRIER_DISABLING_ACTIVE = "iflytek.intent.action.DISABLING_ACTIVE";
    private static final String ACTION_CARRIER_DISABLING_NEGATIVE = "iflytek.intent.action.DISABLING_NEGATIVE";

    private static final int MSG_CARRIER_AUTO_ENABLING = 0x2001;
    private static final int MSG_CARRIER_AUTO_ENABLED = 0x2002;
    private static final int MSG_CARRIER_AUTO_DISABLING = 0x2003;
    private static final int MSG_CARRIER_AUTO_DISABLED = 0x2004;
    private static final int MSG_CARRIER_SCAN_COMPLETED = 0x2005;
    private static final int MSG_CARRIER_SELECTION_DONE = 0x2006;
    private static final int MSG_CARRIER_AUTO_SELECT_DONE = 0x2007;

    private static final int QUERY_OK = 0;
    private static final int SELECT_OK = 0;

    private ToggleButton mMobileSwitchBtn;

    private TextView mBtLinkedDeviceTv;
    private TextView mBtSubNameTv;
    private BtDeviceListAdapter mBtListAdapter;

    private ToggleButton mWifiSwitchBtn;
    private WlanListAdapter mWifiListAdpt;

    private WlanSetting mWlanSetting;
    private BluetoothSetting mBtSetting;
    private Button mBtRescanBtn;
    private ObjectAnimator mBtRescanBtnAnim;

    private HashMap<Integer, AccessPoint> mAccessPoints = new HashMap<>();
    private int mCurrentAccessPointId = 0;
    private TextView mConnectedWifiTv;
    private TextView mApNameTv;
    private TextView mApPasswdTv;
    private Button mApModifyPasswdBtn;
    private ToggleButton mApSwitchTbn;
    private TextView mApDescTv;
    //private ToggleButton mBtAutoConnectSwitchTbn;
    private TextView mCarrierDescTv;
    private ToggleButton mCarrierSwitchTbn;
    private InnerListView mCarriersLv;
    private INetworkQueryService mNetworkQueryService = null;
    private List<OperatorInfo> mNetworkList = new ArrayList<>();
    private int mCurrentCarrier = -1;

    private int mWifiState = WifiManager.WIFI_STATE_UNKNOWN;
    private int mWifiApState = FlyWifiManager.WIFI_AP_STATE_FAILED;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_CARRIER_AUTO_ENABLING:
                    LoadingAnimView.show(getActivity(), getString(R.string.net_carrier_register));
                    mCarrierSwitchTbn.setEnabled(false);
                    try {
                        mNetworkQueryService.setNetworkSelectionModeAutomatic(mSelectCallback);
                    } catch (RemoteException e) {
                        FlyLog.e(TAG, e);
                    }
                    break;
                case MSG_CARRIER_AUTO_ENABLED:
                    LoadingAnimView.hide();
                    mCarriersLv.setAdapter(new CommonRadioGroupAdpt(getActivity()));
                    Notice.getInstatnce().show(getString(R.string.net_carrier_register_pass), true);
                    break;
                case MSG_CARRIER_AUTO_DISABLING:
                    mCarrierSwitchTbn.setEnabled(false);
                    PendingIntent act = PendingIntent.getBroadcast(getActivity(),
                            R.string.net_carrier_active_requestcode,
                            new Intent(ACTION_CARRIER_DISABLING_ACTIVE),
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    PendingIntent neg = PendingIntent.getBroadcast(getActivity(),
                            R.string.net_carrier_negative_requestcode,
                            new Intent(ACTION_CARRIER_DISABLING_NEGATIVE),
                            PendingIntent.FLAG_UPDATE_CURRENT);
                    Notice.getInstatnce().show(getString(R.string.net_carrier_close_tip), false, act, neg);
                    break;
                case MSG_CARRIER_AUTO_DISABLED:
                    LoadingAnimView.show(getActivity(), getString(R.string.net_carrier_search));
                    if (mNetworkQueryService == null) {
                        Intent mNetworkQuery = new Intent();
                        mNetworkQuery.setClassName("com.android.phone", "com.android.phone.NetworkQueryService");
                        getActivity().getApplicationContext().startService(mNetworkQuery);
                        getActivity().getApplicationContext().bindService(mNetworkQuery, mNetworkQueryServiceConnection, Context.BIND_AUTO_CREATE);
                    } else {
                        try {
                            mNetworkQueryService.startNetworkQuery(mQueryCallback);
                        } catch (RemoteException e) {
                            FlyLog.e(TAG, e);
                        }
                    }
                    break;
                case MSG_CARRIER_SCAN_COMPLETED:
                    FlyLog.d(TAG, "Query for carriers is done.");
                    LoadingAnimView.hide();
                    handleQueryResults((List<OperatorInfo>) msg.obj, msg.arg1);
                    break;
                case MSG_CARRIER_SELECTION_DONE:
                    LoadingAnimView.hide();
                    int result = msg.arg1;
                    if (result == SELECT_OK) {
                        mCarriersLv.setItemStatus(mCurrentCarrier, 2);
                        mCarriersLv.setItemChecked(mCurrentCarrier, true);
                        Notice.getInstatnce().show(getString(R.string.net_carrier_register_pass), true);
                    } else {
                        mCarriersLv.setItemStatus(mCurrentCarrier, 0);
                        mCarriersLv.setItemChecked(mCurrentCarrier, false);
                        Notice.getInstatnce().show(getString(R.string.net_carrier_register_fail), true);
                        mCarrierSwitchTbn.performClick();
                    }
                    break;
                case MSG_CARRIER_AUTO_SELECT_DONE:
                    mCarrierSwitchTbn.setEnabled(true);
                    if (msg.arg1 == SELECT_OK) {
                        mHandler.obtainMessage(MSG_CARRIER_AUTO_ENABLED).sendToTarget();
                    } else {
                        mCarrierSwitchTbn.setChecked(false);
                        LoadingAnimView.hide();
                        Notice.getInstatnce().show(getString(R.string.net_carrier_register_fail), true);
                    }
                    break;
            }
        }
    };

    private final ServiceConnection mNetworkQueryServiceConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder service) {
            FlyLog.d(TAG, "connection created, binding phone.NetworkQuery service.");
            mNetworkQueryService = INetworkQueryService.Stub.asInterface(service);
            try {
                mNetworkQueryService.startNetworkQuery(mQueryCallback);
            } catch (RemoteException e) {
                FlyLog.e(TAG, e);
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            FlyLog.d(TAG, "connection disconnected, cleaning local binding.");
            mNetworkQueryService = null;
        }
    };

    private final INetworkQueryServiceCallback mQueryCallback = new INetworkQueryServiceCallback.Stub() {
        @Override
        public void onQueryComplete(List<OperatorInfo> networkInfoArray, int status) {
            mHandler.obtainMessage(MSG_CARRIER_SCAN_COMPLETED, status, 0, networkInfoArray).sendToTarget();
        }
    };

    private final INetworkSelectCallback mSelectCallback = new INetworkSelectCallback.Stub() {
        @Override
        public void onSelectionComplete(int result) {
            mHandler.obtainMessage(MSG_CARRIER_SELECTION_DONE, result, 0).sendToTarget();
        }

        @Override
        public void onAutoSelectComplete(int result) {
            mHandler.obtainMessage(MSG_CARRIER_AUTO_SELECT_DONE, result, 0).sendToTarget();
        }
    };

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (ACTION_CARRIER_DISABLING_ACTIVE.equals(action)) {
                mHandler.obtainMessage(MSG_CARRIER_AUTO_DISABLED).sendToTarget();
                mCarrierSwitchTbn.setEnabled(true);
            } else if (ACTION_CARRIER_DISABLING_NEGATIVE.equals(action)) {
                mCarrierSwitchTbn.setChecked(true);
                mCarrierSwitchTbn.setEnabled(true);
            } else if (TelephonyIntents.SPN_STRINGS_UPDATED_ACTION.equals(action)) {
                updateNetworkName(intent.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_SPN, false),
                        intent.getStringExtra(TelephonyIntents.EXTRA_SPN),
                        intent.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_PLMN, false),
                        intent.getStringExtra(TelephonyIntents.EXTRA_PLMN));
            }

        }
    };

    private static String getStateDesc(int state) {
        switch (state) {
            case WifiManager.WIFI_STATE_ENABLING:
                return "ENABLING";
            case WifiManager.WIFI_STATE_ENABLED:
                return "ENABLED";
            case WifiManager.WIFI_STATE_DISABLING:
                return "DISABLING";
            case WifiManager.WIFI_STATE_DISABLED:
                return "DISABLED";
            default:
                return "UNKNOWN";
        }
    }

    private String getApStateDesc(int state) {
        switch (state) {
            case FlyWifiManager.WIFI_AP_STATE_DISABLING:
                return "AP_DISABLING";
            case FlyWifiManager.WIFI_AP_STATE_DISABLED:
                return "AP_DISABLED";
            case FlyWifiManager.WIFI_AP_STATE_ENABLING:
                return "AP_ENABLING";
            case FlyWifiManager.WIFI_AP_STATE_ENABLED:
                return "AP_ENABLED";
            case FlyWifiManager.WIFI_AP_STATE_FAILED:
                return "AP_FAILED";
        }
        return "UNKNOWN";
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        init(view);
        return view;
    }

    @Override
    public void onDestroyView() {
        stopScanAnim();
        mBtSetting.fini();
        getActivity().unregisterReceiver(mReceiver);
        if (mNetworkQueryService != null) {
            getActivity().getApplicationContext().unbindService(mNetworkQueryServiceConnection);
            mNetworkQueryService = null;
        }
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        FlyLog.d(TAG, "enter");
        ConnectivityManager cm =
                (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        mMobileSwitchBtn.setChecked(cm.getMobileDataEnabled());
        mBtSetting.onResume();
        mWlanSetting.onResume();
        if (!mWlanSetting.isWifiEnabled()) {
            BottomMenu.clear();
        }
        mWifiState = mWlanSetting.getManager().getWifiState();
        mWifiApState = mWlanSetting.getManager().getWifiApState();
        super.onResume();
    }

    @Override
    public void onPause() {
        stopScanAnim();
        mBtSetting.stopScan();
        mWlanSetting.onPause();
        super.onPause();
    }

    @Override
    public void renew() {
    }

    @Override
    public void processCommandline(final String cmd) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FlyLog.d(TAG, "cmd=" + cmd);
                int retry = 10;
                while (retry-- > 0) {

                    if (mWlanSetting == null) {
                        mWlanSetting = new WlanSetting();
                    }
                    if (mBtSetting == null) {
                        mBtSetting = new BluetoothSetting();
                    }
                    if (mWlanSetting.ready() && mBtSetting.ready()) {

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                FlyLog.d(TAG, "excute " + cmd);
                                switch (cmd) {
                                    case "btopen":
                                        FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_bluetooth), "broadcast");
                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                try {
                                                    Thread.sleep(300);
                                                } catch (InterruptedException e) {
                                                    FlyLog.e(TAG, e);
                                                }
                                                getActivity().runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        expand(0);
                                                    }
                                                });
                                            }
                                        }).start();
                                        break;
                                    case "wifiopen":
                                        FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_wifi), "broadcast");

                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                try {
                                                    Thread.sleep(300);
                                                } catch (InterruptedException e) {
                                                    FlyLog.e(TAG, e);
                                                }
                                                getActivity().runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        expand(1);
                                                    }
                                                });
                                            }
                                        }).start();
                                        break;
                                    case "btscan":

                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                try {
                                                    Thread.sleep(300);
                                                } catch (InterruptedException e) {
                                                    FlyLog.e(TAG, e);
                                                }
                                                getActivity().runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        expand(0);
                                                    }
                                                });
                                            }
                                        }).start();
                                        FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_bt_scan), "broadcast");
                                        mBtRescanBtn.performClick();
                                        break;
                                    case "btdisconnect":
                                        // TODO
                                        break;
                                    case "wifion":
                                        if (!mWlanSetting.isWifiEnabled()) {
                                            FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_wifi_switch), "true,broadcast");
                                            mWifiSwitchBtn.performClick();
                                        }
                                        break;
                                    case "wifioff":
                                        if (mWlanSetting.isWifiEnabled()) {
                                            FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_wifi_switch), "false,broadcast");
                                            mWifiSwitchBtn.performClick();
                                        }
                                        break;
                                    default:
                                        FlyLog.w(TAG, "unknown cmd:" + cmd);
                                }
                            }
                        });
                        return;
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        FlyLog.e(TAG, e);
                    }
                }

                FlyLog.e(TAG, "not excuted " + cmd);

            }
        }).start();

    }

    @Override
    public String[] getCommands() {
        return CMD;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        final String result;
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (data == null) {
            return;
        }
        Bundle bundle = data.getExtras();
        if (bundle != null) {
            switch (requestCode) {
                case REQUEST_BT_NAME:
                    result = bundle.getString(EditActivity.RESULT, "");
                    if (!TextUtils.isEmpty(result)) {
                        mBtSetting.setName(result);
                    }
                    return;
                case REQUEST_NET_PASSWD:
                    result = bundle.getString(EditActivity.RESULT, "");
                    boolean needConnect = bundle.getBoolean(NetPasswdEditActivity.NEED_CONNECT, false);
                    int apId = bundle.getInt(NetPasswdEditActivity.AP_ID, -1);
                    AccessPoint bean = null;
                    synchronized (mAccessPoints) {
                        bean = mAccessPoints.get(apId);
                        mAccessPoints.remove(apId);
                    }
                    if (bean == null) {
                        return;
                    }
                    mWlanSetting.saveConfig(bean, result, needConnect);
                    return;
                case REQUEST_AP_PASSWD:
                    result = bundle.getString(EditActivity.RESULT, "");
                    if (!TextUtils.isEmpty(result)) {
                        onApPasswd(result);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                mWlanSetting.setApPasswd(result);
                            }
                        }).start();
                    }
                    return;
                default:
                    FlyLog.e(TAG, "unexpected: requestCode=" + requestCode + ",resultCode=" + resultCode + ",data=" + data);
            }
        } else {
            if (requestCode == REQUEST_NET_PASSWD) {
                collapse(1);
            }
        }
    }

    private void init(View view) {
        addUnit(R.layout.bluetooth, R.layout.bluetooth_sub,
                new OnExpandChangeListener() {
                    @Override
                    public void onExpandChanged(boolean isExpanded) {
                        if (!isExpanded) {
                            mBtLinkedDeviceTv.setVisibility(View.VISIBLE);
                            mBtRescanBtn.setVisibility(View.GONE);
                            setConnectDeviceName(mBtSetting.getLinkedDEviceName());
                        } else {
                            mBtLinkedDeviceTv.setVisibility(View.GONE);
                            mBtRescanBtn.setVisibility(View.VISIBLE);
                            mBtSetting.updateInfo(false);
                        }
                    }
                }, null);
        addUnit(R.layout.wifi, R.layout.wifi_sub, new OnExpandChangeListener() {
            @Override
            public void onExpandChanged(boolean isExpanded) {
                if (isExpanded) {
                    mConnectedWifiTv.setVisibility(View.GONE);
                } else {
                    mConnectedWifiTv.setVisibility(View.VISIBLE);
                }
            }
        }, null);
        addUnit(R.layout.ap, R.layout.ap_sub, new OnExpandChangeListener() {
            @Override
            public void onExpandChanged(boolean isExpanded) {
                if (isExpanded) {
                    mApDescTv.setVisibility(View.GONE);
                } else {
                    mApDescTv.setVisibility(View.VISIBLE);
                }
            }
        }, null);
        addUnit(R.layout.mobile_network);
        addUnit(R.layout.carrier, R.layout.carrier_sub, new OnExpandChangeListener() {
            @Override
            public void onExpandChanged(boolean isExpanded) {
                if (isExpanded) {
                    mCarrierDescTv.setVisibility(View.GONE);
                    if (!mCarrierSwitchTbn.isChecked()) {
                        mHandler.obtainMessage(MSG_CARRIER_AUTO_DISABLED).sendToTarget();
                    }
                } else {
                    mCarrierDescTv.setVisibility(View.VISIBLE);
                }
            }
        }, null);
        // FIXME: 2016/1/4
        // temp solution.
        // 点击最后一个item，切换，再点开，箭头状态不对，临时添加一个不可见item解决一下
        addHiddenUnit(R.layout.aboutsystem);

        mMobileSwitchBtn = (ToggleButton) view.findViewById(R.id.net_mobile_switch_tbn);
        mMobileSwitchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            }
        });
        mMobileSwitchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isChecked = mMobileSwitchBtn.isChecked();
                ConnectivityManager cm =
                        (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
                cm.setMobileDataEnabled(isChecked);
            }
        });

        RelativeLayout btNameRl = (RelativeLayout) view.findViewById(R.id.net_bt_name_rl);
        btNameRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BtNameEditActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(EditActivity.NAME, getString(R.string.net_bt_modify_name));
                bundle.putString(EditActivity.ORI_VALUE, mBtSetting.getName());
                intent.putExtras(bundle);
                startActivityForResult(intent, REQUEST_BT_NAME);
            }
        });
        btNameRl.setClickable(false);


        mBtLinkedDeviceTv = (TextView) view.findViewById(R.id.net_bt_linkeddevice_tv);
        mBtRescanBtn = (Button) view.findViewById(R.id.net_bt_rescan_btn);
        mBtRescanBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_bt_scan));
                mBtListAdapter.clearScanedList();
                startScanAnim();
                mBtSetting.scan();
            }
        });

        mBtSubNameTv = (TextView) view.findViewById(R.id.net_bt_name_tv);
//        mBtAutoConnectSwitchTbn = (ToggleButton) view.findViewById(R.id.net_bt_auto_connect_on_boot_tbn);
//        mBtAutoConnectSwitchTbn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                mBtSetting.setAutoConnect(isChecked);
//            }
//        });

        ListView usefulDeviceLv = (ListView) view
                .findViewById(R.id.net_bt_devicelist_lv);
        mBtListAdapter = new BtDeviceListAdapter(getActivity());
        usefulDeviceLv.setAdapter(mBtListAdapter);
        usefulDeviceLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
                final BtDeviceBean bean = (BtDeviceBean) mBtListAdapter.getItem(position);
                FlyLog.d(TAG, "bean=" + bean.getName() + ",state=" + bean.getState());

                if (bean.getState() == BluetoothServiceCons.ConnectionStatus.CONNECTION_CONNECTING) {
                    return true;
                }
                final ImageView iv = (ImageView) v.findViewById(R.id.net_bt_item_selected_iv);
                final TextView deviceStatusTv = (TextView) v.findViewById(R.id.net_bt_device_status_tv);
                final Button deviceActionBtn = (Button) v.findViewById(R.id.net_bt_device_action_btn);

                iv.setVisibility(View.VISIBLE);
                deviceStatusTv.setVisibility(View.GONE);
                deviceActionBtn.setVisibility(View.GONE);

                PopupWindow.OnDismissListener dismissListener = new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        iv.setVisibility(View.GONE);
                        deviceStatusTv.setVisibility(View.VISIBLE);
                        deviceActionBtn.setVisibility(View.VISIBLE);
                    }
                };

                switch (bean.getState()) {
                    case BluetoothServiceCons.ConnectionStatus.CONNECTION_CONNECT_FAILED:
                    case BluetoothServiceCons.ConnectionStatus.CONNECTION_DISCONNECT:
                        new BottomMenu(getActivity(), R.layout.menu_disconnect_bt)
                                .setButtonListener(R.id.menu_connect_bt, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_bluetooth) + "-" + getString(R.string.menu_connect_bt), "long_click");
                                        mBtSetting.connect(bean);
                                    }
                                })
                                .setButtonListener(R.id.menu_unpair_bt, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_bluetooth) + "-" + getString(R.string.menu_unpair_bt), "long_click");
                                        mBtSetting.unpair(bean);
                                    }
                                })
                                .setOnDismissListener(dismissListener)
                                .show();
                        break;
                    case BluetoothServiceCons.ConnectionStatus.CONNECTION_CONNECTED:
                        new BottomMenu(getActivity(), R.layout.menu_connected_bt)
                                .setButtonListener(R.id.menu_disconnect_bt, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_bluetooth) + "-" + getString(R.string.menu_disconnect_bt), "long_click");
                                        mBtSetting.cancelConnect(bean);
                                    }
                                })
                                .setButtonListener(R.id.menu_unpair_bt, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_bluetooth) + "-" + getString(R.string.menu_unpair_bt), "long_click");
                                        mBtSetting.unpair(bean);
                                    }
                                })
                                .setOnDismissListener(dismissListener)
                                .show();
                        break;
                    case BluetoothServiceCons.ConnectionStatus.CONNECTION_CONNECTING:
                        break;
                    case BluetoothServiceCons.ConnectionStatus.CONNECTION_UNPAIRED:
                        new BottomMenu(getActivity(), R.layout.menu_unpaired_bt)
                                .setButtonListener(R.id.menu_connect_bt, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_bluetooth) + "-" + getString(R.string.menu_connect_bt), "long_click");
                                        mBtSetting.connect(bean);
                                    }
                                })
                                .setOnDismissListener(dismissListener)
                                .show();
                        break;
                    default:
                        FlyLog.e(TAG, "unknown status[" + bean.getState() + "]");
                }
                return true;
            }
        });


        mWifiSwitchBtn = (ToggleButton) view.findViewById(R.id.net_wifi_switch_tbn);
        mWifiSwitchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isEnabled = mWifiSwitchBtn.isChecked();
                FlowerCollector.onEvent(getActivity().getApplicationContext(), getString(R.string.networking) + "-" + getString(R.string.net_wifi_switch), "" + isEnabled);
                mWlanSetting.setWifiEnabled(isEnabled);
                onWifiStateChanged(!isEnabled ? WifiManager.WIFI_STATE_DISABLING : WifiManager.WIFI_STATE_ENABLING);
            }
        });
        mConnectedWifiTv = (TextView) view.findViewById(R.id.net_wifi_connect_wifi_tv);
        mWifiListAdpt = new WlanListAdapter(getActivity());
        ListView wifiList = (ListView) view
                .findViewById(R.id.net_wifi_wlan_list_lv);
        wifiList.setAdapter(mWifiListAdpt);

        mApSwitchTbn = (ToggleButton) view.findViewById(R.id.net_ap_switch_tbn);
        mApSwitchTbn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final boolean isEnabled = mApSwitchTbn.isChecked();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mWlanSetting.setApEnabled(isEnabled);
                        onApStateChanged(!isEnabled
                                ? FlyWifiManager.WIFI_AP_STATE_DISABLING
                                : FlyWifiManager.WIFI_AP_STATE_ENABLING);
                    }
                }).start();
            }
        });
        mApNameTv = (TextView) view.findViewById(R.id.net_ap_name_tv);
        mApDescTv = (TextView) view.findViewById(R.id.net_ap_desc_tv);
        mApPasswdTv = (TextView) view.findViewById(R.id.net_ap_passwd_tv);
        mApModifyPasswdBtn = (Button) view.findViewById(R.id.net_ap_passwd_btn);
        mApModifyPasswdBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ApPasswdEditActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(EditActivity.NAME, getString(R.string.net_ap_passwd_modify));
                bundle.putString(EditActivity.HINT_VALUE, getString(R.string.net_ap_passwd_unchanged));
                bundle.putString(EditActivity.ORI_VALUE, mApPasswdTv.getText().toString());
                bundle.putInt(EditActivity.INPUT_TYPE, EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                intent.putExtras(bundle);
                startActivityForResult(intent, REQUEST_AP_PASSWD);
            }
        });

        mCarrierDescTv = (TextView) view.findViewById(R.id.net_carrier_value_tv);
        mCarrierSwitchTbn = (ToggleButton) view.findViewById(R.id.net_carrier_switch_tbn);
        mCarrierSwitchTbn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            }
        });
        mCarrierSwitchTbn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCarrierSwitchTbn.isChecked()) {
                    mHandler.obtainMessage(MSG_CARRIER_AUTO_ENABLING).sendToTarget();
                } else {
                    mHandler.obtainMessage(MSG_CARRIER_AUTO_DISABLING).sendToTarget();
                }
            }
        });
        mCarriersLv = (InnerListView) view.findViewById(R.id.net_carrier_carriers_lv);
        mCarriersLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mCurrentCarrier != position) {
                    LoadingAnimView.show(getActivity(), getString(R.string.net_carrier_register));
                    mCurrentCarrier = position;
                    mCarriersLv.setItemStatus(position, 1);
                    mCarriersLv.setItemChecked(position, true);
                    try {
                        mNetworkQueryService.selectNetworkManually(mSelectCallback, mNetworkList.get(position));
                    } catch (RemoteException e) {
                        FlyLog.e(TAG, e);
                    }
                }
            }
        });

        if (mWlanSetting == null) {
            mWlanSetting = new WlanSetting();
        }
        mWlanSetting.init(getActivity(), this);
        mWifiListAdpt.setWlanMngr(mWlanSetting);

        if (mBtSetting == null) {
            mBtSetting = new BluetoothSetting();
        }
        mBtSetting.init(getActivity(), this);
        mBtListAdapter.setBtMngr(mBtSetting);

        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ACTION_CARRIER_DISABLING_ACTIVE);
        mFilter.addAction(ACTION_CARRIER_DISABLING_NEGATIVE);
        mFilter.addAction(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION);
        getActivity().registerReceiver(mReceiver, mFilter);

    }

    @Override
    public void onWifiStateChanged(final int state) {
        FlyLog.d(TAG, "lastState = " + getStateDesc(mWifiState) + " -> state = " + getStateDesc(state));
        mWifiState = state;
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                switch (state) {
                    case WifiManager.WIFI_STATE_ENABLING:
                        mWifiSwitchBtn.setEnabled(false);
                        mApSwitchTbn.setEnabled(false);
                        collapse(1);
                        setExpandable(1, false);
                        mConnectedWifiTv.setText(getString(R.string.net_wifi_no_wifi));
                        BottomMenu.clear();
                        break;
                    case WifiManager.WIFI_STATE_ENABLED:
                        mWifiSwitchBtn.setChecked(true);
                        mWifiSwitchBtn.setEnabled(true);
                        mApSwitchTbn.setEnabled(true);
                        setExpandable(1, true);
                        break;
                    case WifiManager.WIFI_STATE_DISABLING:
                        mWifiSwitchBtn.setEnabled(false);
                        mApSwitchTbn.setEnabled(false);
                        collapse(1);
                        setExpandable(1, false);
                        mConnectedWifiTv.setText(getString(R.string.net_wifi_no_wifi));
                        BottomMenu.clear();
                        break;
                    case WifiManager.WIFI_STATE_DISABLED:
                        mWifiSwitchBtn.setChecked(false);
                        if (mWifiApState != FlyWifiManager.WIFI_AP_STATE_ENABLING) {
                            mWifiSwitchBtn.setEnabled(true);
                            mApSwitchTbn.setEnabled(true);
                        }
                        setExpandable(1, false);
                        collapse(1);
                        mConnectedWifiTv.setText(getString(R.string.net_wifi_no_wifi));
                        BottomMenu.clear();
                        break;
                    default:
                        mWifiSwitchBtn.setChecked(false);
                        mWifiSwitchBtn.setEnabled(true);
                        mApSwitchTbn.setEnabled(true);
                        setExpandable(1, false);
                        collapse(1);
                        mConnectedWifiTv.setText(getString(R.string.net_wifi_no_wifi));
                        BottomMenu.clear();
                        break;
                }
            }
        });

    }

    @Override
    public void setWifiListItems(ArrayList<AccessPoint> accessPoints) {
        mWifiListAdpt.setItems(accessPoints);
        mWifiListAdpt.notifyDataSetChanged();
    }

    @Override
    public ArrayList<AccessPoint> getListItems() {
        return (ArrayList<AccessPoint>) mWifiListAdpt.getItems();
    }

    @Override
    public void popPasswdWindow(AccessPoint bean, boolean needConnect, boolean modify) {
        Intent intent = new Intent(getActivity(), NetPasswdEditActivity.class);
        Bundle bundle = new Bundle();

        bundle.putInt(EditActivity.INPUT_TYPE, EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
        if (modify) {
            bundle.putString(EditActivity.NAME, getString(R.string.net_wifi_modify_network));
            bundle.putString(EditActivity.HINT_VALUE, getString(R.string.net_wifi_passwd_unchanged));
        } else {
            bundle.putString(EditActivity.NAME, getString(R.string.net_wifi_setup_network));
            bundle.putString(EditActivity.HINT_VALUE, getString(R.string.net_wifi_passwd_unseted));
        }

        bundle.putBoolean(NetPasswdEditActivity.NEED_CONNECT, needConnect);
        bundle.putInt(NetPasswdEditActivity.NET_TYPE, bean.getSecurity());
        bundle.putString(NetPasswdEditActivity.WLAN_NAME, bean.ssid);
        int[] resid = {R.string.net_wifi_rssi_strength_weak, R.string.net_wifi_rssi_strength_normal, R.string.net_wifi_rssi_strength_good, R.string.net_wifi_rssi_strength_strong, R.string.net_wifi_rssi_strength_unknow};
        int signalLevel = bean.getLevel();
        String signalLevelStr;
        if (signalLevel >= 0 && signalLevel < 4) {
            signalLevelStr = getString(resid[signalLevel]);
        } else {
            signalLevelStr = getString(resid[4]);
        }
        bundle.putString(NetPasswdEditActivity.SIGNAL_LEVEL, signalLevelStr);
        int id = 0;
        synchronized (mAccessPoints) {
            id = mCurrentAccessPointId;
            mCurrentAccessPointId++;
            mAccessPoints.put(id, bean);
        }

        bundle.putInt(NetPasswdEditActivity.AP_ID, id);

        intent.putExtras(bundle);
        startActivityForResult(intent, REQUEST_NET_PASSWD);
    }

    @Override
    public void updateConntecedWifiName(String name) {
        if (name == null) {
            name = getString(R.string.net_wifi_no_wifi);
        }
        final String nameFinal = name;

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String displayName = nameFinal;
                if (displayName.length() > 20) {
                    displayName = displayName.substring(0, 17) + "...";
                }
                mConnectedWifiTv.setText(displayName);
            }
        });
    }

    @Override
    public void onApStateChanged(final int state) {
        FlyLog.d(TAG, "lastState = " + getApStateDesc(mWifiApState) + " -> state = " + getApStateDesc(state));
        mWifiApState = state;
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                switch (state) {
                    case FlyWifiManager.WIFI_AP_STATE_ENABLING:
                        mApSwitchTbn.setEnabled(false);
                        mWifiSwitchBtn.setEnabled(false);
                        collapse(2);
                        setExpandable(2, false);
                        BottomMenu.clear();
                        break;
                    case FlyWifiManager.WIFI_AP_STATE_ENABLED:
                        mApSwitchTbn.setChecked(true);
                        mApSwitchTbn.setEnabled(true);
                        mWifiSwitchBtn.setEnabled(true);
                        setExpandable(2, true);
                        break;
                    case FlyWifiManager.WIFI_AP_STATE_DISABLING:
                        mApSwitchTbn.setEnabled(false);
                        mWifiSwitchBtn.setEnabled(false);
                        collapse(2);
                        setExpandable(2, false);
                        BottomMenu.clear();
                        break;
                    case FlyWifiManager.WIFI_AP_STATE_DISABLED:
                        mApSwitchTbn.setChecked(false);
                        if (mWifiState != WifiManager.WIFI_STATE_ENABLING) {
                            mApSwitchTbn.setEnabled(true);
                            mWifiSwitchBtn.setEnabled(true);
                        }
                        setExpandable(2, false);
                        collapse(2);
                        BottomMenu.clear();
                        break;
                    default:
                        mApSwitchTbn.setChecked(false);
                        mApSwitchTbn.setEnabled(true);
                        mWifiSwitchBtn.setEnabled(true);
                        setExpandable(2, false);
                        collapse(2);
                        BottomMenu.clear();
                        break;
                }
            }
        });

    }

    @Override
    public void onApName(final String name) {
        if (!TextUtils.isEmpty(name)) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mApNameTv.setText(name);
                }
            });
        }
    }

    @Override
    public void onApPasswd(final String passwd) {
        if (!TextUtils.isEmpty(passwd)) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mApPasswdTv.setText(passwd);
                }
            });
        }
    }

    @Override
    public void onOutOfTraffic(final int acticeStatus) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(new ComponentName("com.iflytek.autofly.account",
                        "com.iflytek.autofly.account.ui.MainActivity"));
                if (acticeStatus == 1) {
                    Notice.getInstatnce().showWithActivity(getString(R.string.traffic_warn), false, intent);
                } else {
                    Notice.getInstatnce().showWithActivity(getString(R.string.traffic_warn_unactive), false, intent);
                }
            }
        });
    }

    @Override
    public void onApEnable() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Notice.getInstatnce().show(getString(R.string.ap_enable_warn), false);
            }
        });

    }


    @Override
    public void onNameChanged(final String btName) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!TextUtils.isEmpty(btName)) {
                    mBtSubNameTv.setText(btName);
                }
            }
        });
    }

    @Override
    public void onPinChanged(String btPin) {
    }

    @Override
    public BtDeviceListAdapter getAdapter() {
        return mBtListAdapter;
    }

    @Override
    public void onAutoConnectChanged(final boolean autoConnect) {
//        getActivity().runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                mBtAutoConnectSwitchTbn.setChecked(autoConnect);
//            }
//        });
    }

    @Override
    public void onDeviceFound() {
        //startScanAnim();
    }

    @Override
    public void onScanStart() {
//        Animation scanning= AnimationUtils.loadAnimation(getActivity(), R.anim.net_bt_scaning);
//        mBtRescanBtn.startAnimation(scanning);
        startScanAnim();
    }

    @Override
    public void onScanEnd() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                stopScanAnim();
            }
        });

    }

    @Override
    public void setConnectDeviceName(final String name) {
        FlyLog.d(TAG, "name=" + name + ",mBtLinkedDeviceTv=" + mBtLinkedDeviceTv);
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!TextUtils.isEmpty(name)) {
                    if (mBtLinkedDeviceTv != null) {
                        String displayName = name;
                        if (displayName.length() > 20) {
                            displayName = displayName.substring(0, 17) + "...";
                        }
                        mBtLinkedDeviceTv.setText(displayName);
                        mBtLinkedDeviceTv.invalidate();
                    }
                } else {
                    if (mBtLinkedDeviceTv != null) {
                        mBtLinkedDeviceTv.setText(getString(R.string.net_bt_nondevice));
                    }
                }
            }
        });
    }

    private void startScanAnim() {
        if (mBtRescanBtnAnim == null) {
            mBtRescanBtnAnim = ObjectAnimator
                    .ofFloat(mBtRescanBtn, "rotation", 0.0F, 359.0F)
                    .setDuration(359 * 3);
            mBtRescanBtnAnim.setInterpolator(new LinearInterpolator());
            mBtRescanBtnAnim.setRepeatCount(ObjectAnimator.INFINITE);
        }
        if (!mBtRescanBtnAnim.isStarted()) {
            //stopScanAnim();
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mBtRescanBtn.setEnabled(false);
                    mBtRescanBtnAnim.start();
                }
            });
        }
    }

    private void stopScanAnim() {
        if (mBtRescanBtnAnim != null) {
            mBtRescanBtnAnim.end();
            mBtRescanBtnAnim = null;
        }
        mBtRescanBtn.setEnabled(true);
    }

    private void handleQueryResults(List<OperatorInfo> result, int status) {
        if (status == QUERY_OK) {
            if (result != null) {
                CommonRadioGroupAdpt mCarrierAdpt = new CommonRadioGroupAdpt(getActivity());
                mNetworkList.clear();
                for (OperatorInfo ni : result) {
                    String title = getNetworkTitle(ni);
                    int resId = convertCarrierName(title);
                    String bean = resId == 0 ? title : getString(resId);
                    // Unknown carrier if resId == 0.
                    mCarrierAdpt.addItem(new CommonRadioButtonBean(bean, resId));
                    mNetworkList.add(ni);
                    if (mCarrierDescTv.getText().toString().equals(bean)) {
                        mCurrentCarrier = mNetworkList.size() - 1;
                    }
                }
                FlyLog.d(TAG, "mCurrentCarrier = " + mCurrentCarrier);
                mCarriersLv.setAdapter(mCarrierAdpt);
                if (mCurrentCarrier != -1) {
                    mCarriersLv.setItemChecked(mCurrentCarrier, true);
                }
            } else {
                // show empty list.
            }
        } else {
            FlyLog.e(TAG, "Error while querying available networks.");
        }
    }

    /**
     * Returns the title of the network obtained in the manual search.
     * @param ni OperatorInfo contains the information of the network.
     * @return Long Name if not null/empty, otherwise Short Name if not null/empty,
     * else MCCMNC string.
     */
    private String getNetworkTitle(OperatorInfo ni) {
        if (!TextUtils.isEmpty(ni.getOperatorAlphaLong())) {
            return ni.getOperatorAlphaLong();
        } else if (!TextUtils.isEmpty(ni.getOperatorAlphaShort())) {
            return ni.getOperatorAlphaShort();
        } else {
            return ni.getOperatorNumeric();
        }
    }

    private void updateNetworkName(boolean showSpn, String spn, boolean showPlmn, String plmn) {
        FlyLog.d(TAG, "updateNetworkName showSpn=" + showSpn + " spn=" + spn
                + " showPlmn=" + showPlmn + " plmn=" + plmn);
        StringBuilder str = new StringBuilder();
        boolean something = false;
        if (showPlmn && plmn != null) {
            str.append(plmn);
            something = true;
        }
        if (showSpn && spn != null) {
            if (something) {
                str.append(getString(R.string.net_carrier_name_separator));
            }
            str.append(spn);
            something = true;
        }
        if (something) {
            mCarrierDescTv.setText(convertCarrierName(str.toString()));
        } else {
            mCarrierDescTv.setText(getString(R.string.net_carrier_name_default));
        }
    }

    private int convertCarrierName(String title) {
        String mName = title.toLowerCase();
        int resId = 0;

        if (mName.contains("mobile")) {
            if (mName.contains("4g")) {
                resId = R.string.net_carrier_name_china_mobile_4g;
            } else if (mName.contains("3g")) {
                resId = R.string.net_carrier_name_china_mobile_3g;
            } else {
                // For now, this is 2G.
                resId = R.string.net_carrier_name_china_mobile_2g;
            }
        } else if (mName.contains("unicom")) {
            if (mName.contains("4g")) {
                resId = R.string.net_carrier_name_china_unicom_4g;
            } else if (mName.contains("3g")) {
                resId = R.string.net_carrier_name_china_unicom_3g;
            } else {
                // For now, this is 2G.
                resId = R.string.net_carrier_name_china_unicom_2g;
            }
        } else if (mName.contains("telecom")) {
            if (mName.contains("4g")) {
                resId = R.string.net_carrier_name_china_telecom_4g;
            } else if (mName.contains("3g")) {
                resId = R.string.net_carrier_name_china_telecom_3g;
            } else {
                // For now, this is 2G.
                resId = R.string.net_carrier_name_china_telecom_2g;
            }
        } else {
            // Unknown...
            FlyLog.w(TAG, "Unkonwn carrier: " + mName);
        }

        return resId;
    }

}
